﻿using Seagull.BarTender.Print;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TB_WinForm.Apple_Box
{
    public partial class YDB : Form
    {
        public YDB()
        {
            InitializeComponent();
        }
        private static string BTW_LOCAL = Application.StartupPath + @"\BarCode\" + "YDB.btw";
        private void YDB_Load(object sender, EventArgs e)
        {
            PrintAddList();
        }

        private void PrintAddList()
        {
            Printers printers = new Printers();
            foreach (Printer printer in printers)
            {
                comboBox1.Items.Add(printer.PrinterName);
            }

            if (printers.Count > 0)
            {
                // Automatically select the default printer.
                comboBox1.SelectedItem = printers.Default.PrinterName;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入商品标题", "警告");
                textBox1.Focus();
            }
            else if (textBox2.Text == "")
            {
                MessageBox.Show("请输入序列号", "警告");
                textBox2.Focus();
            }
            else if (textBox3.Text == "")
            {
                MessageBox.Show("请扫描红包二维码", "警告");
                textBox3.Focus();
            }
            else
            {
                try
                {
                    LabelFormatDocument labelFormat;
                    using (Engine btEngine = new Engine(true))
                    {
                        labelFormat = btEngine.Documents.Open(BTW_LOCAL.ToString());//载入模板
                        labelFormat.SubStrings["QR_URL"].Value = textBox3.Text.Trim().ToString();
                        labelFormat.SubStrings["TITLE"].Value = textBox1.Text.Trim().ToString();
                        labelFormat.SubStrings["SN"].Value = textBox2.Text.Trim().ToString();

                        labelFormat.PrintSetup.PrinterName = comboBox1.Text.Trim().ToString();
                        labelFormat.PrintSetup.IdenticalCopiesOfLabel = 1;//打印数量
                        Random RD = new Random();
                        labelFormat.Print(RD.Next(111111,999999).ToString() + DateTime.Now.ToString(), 1);//开始打印
                        
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                textBox2.Text = "";
                textBox3.Text = "";
                textBox2.Focus();
            }
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter)
            {
                textBox3.Focus();
            }
        }
    }
}
